<?php

namespace app\common\model;

use think\Model;


class Article extends Model
{

    

    

    // 表名
    protected $name = 'article';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'status_text',
        'is_g_top_text',
        'is_s_top_text',
        'g_endtime_text',
        's_endtime_text'
    ];
    

    
    public function getStatusList()
    {
        return ['0' => __('Status 0')];
    }

    public function getIsGTopList()
    {
        return ['0' => __('Is_g_top 0'), '1' => __('Is_g_top 1')];
    }

    public function getIsSTopList()
    {
        return ['0' => __('Is_s_top 0'), '1' => __('Is_s_top 1')];
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getIsGTopTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_g_top']) ? $data['is_g_top'] : '');
        $list = $this->getIsGTopList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getIsSTopTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_s_top']) ? $data['is_s_top'] : '');
        $list = $this->getIsSTopList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getGEndtimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['g_endtime']) ? $data['g_endtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getSEndtimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['s_endtime']) ? $data['s_endtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setGEndtimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setSEndtimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function provincial()
    {
        return $this->belongsTo('Provincial', 'provincial_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
